package com.like.widget;

import android.text.InputFilter;
import android.text.Spanned;

public class MoneyInputFilter implements InputFilter {

    public static final int DECIMAL_DIGITS = 2;

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        // 删除等特殊字符，直接返回
        if ("".equals(source.toString())) {
            return null;
        }
        String dValue = dest.toString() + source;
        String[] splitArray = dValue.split("\\.");
        if (splitArray.length > 1) {
            String dotValue = splitArray[1];
            int diff = dotValue.length() - DECIMAL_DIGITS;
            if (diff > 0) {
                return dValue.subSequence(start, end - diff);
            }
        }
        return null;
    }
}
